package pe.edu.upc.dew.egretel.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import pe.edu.upc.dew.egretel.model.Persona;
import pe.edu.upc.dew.egretel.util.JdbcUtils;

public class PersonaDaoJdbc implements PersonaDao {

    public Persona getPersonaPorLoginName(String loginName) {

        try {

            Persona persona = getPersona(loginName);
            ReclamoDao reclamoDao = new ReclamoDaoJdbc();

            if (persona.getIdTipoPersona().equals("E")) {
                persona.setReclamos(reclamoDao.getReclamosPorIdEspecialista(String.valueOf(persona.getIdPersona())));
            } else {
                persona.setReclamos(reclamoDao.getReclamosPorIdCliente(String.valueOf(persona.getIdPersona())));
            }

            return persona;

        } catch (Exception ex) {
            Logger.getLogger(PersonaDaoJdbc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Persona getPersonaPorLoginName_old(String loginName) {

        Connection conn = JdbcUtils.getConnection();

        try {
            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select username, password, nombre from Persona where username ='" + loginName + "'");

            if (rs.next()) {
                Persona persona = new Persona();
                // nombre de la columna
                persona.setUserName(rs.getString("username"));
                persona.setPassword(rs.getString("password"));
                persona.setNombre(rs.getString("nombre"));
                return persona;
            }

        } catch (SQLException ex) {
            Logger.getLogger(PersonaDaoJdbc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Persona getPersona(String loginName) {

        Connection conn = JdbcUtils.getConnection();

        try {
            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select IdPersona, nombre, IdTipoPersona, username, password, telefono, cargo, email from Persona where username ='" + loginName + "'");

            if (rs.next()) {

                Persona persona = new Persona();

                // nombre de la columna
                persona.setIdPersona(Integer.parseInt(rs.getString("idPersona")));
                persona.setNombre(rs.getString("nombre"));
                persona.setIdTipoPersona(rs.getString("IdTipoPersona"));
                persona.setUserName(rs.getString("username"));
                persona.setPassword(rs.getString("password"));
                persona.setTelefono(rs.getString("telefono"));
                persona.setCargo(rs.getString("cargo"));
                persona.setEmail(rs.getString("email"));

                return persona;
            }

        } catch (SQLException ex) {
            Logger.getLogger(PersonaDaoJdbc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Persona getPersonaId(String idPersona) {

        Connection conn = JdbcUtils.getConnection();

        try {
            Statement st = conn.createStatement();

            ResultSet rs = st.executeQuery("select IdPersona, nombre, IdTipoPersona, username, password, telefono, cargo, email from Persona where CAST(IdPersona AS CHAR(10)) ='" + idPersona + "'");

            if (rs.next()) {

                Persona persona = new Persona();

                // nombre de la columna
                persona.setIdPersona(Integer.parseInt(rs.getString("idPersona")));
                persona.setNombre(rs.getString("nombre"));
                persona.setIdTipoPersona(rs.getString("IdTipoPersona"));
                persona.setUserName(rs.getString("username"));
                persona.setPassword(rs.getString("password"));
                persona.setTelefono(rs.getString("telefono"));
                persona.setCargo(rs.getString("cargo"));
                persona.setEmail(rs.getString("email"));

                return persona;
            }

        } catch (SQLException ex) {
            Logger.getLogger(PersonaDaoJdbc.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
